<?php
/**
 * 《通信录》实现类
 *
 * @author 孙晓晔
 * @version $Id: Work.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/Info.class.php');

class Ldap extends Info {
	public $tbl = Constant::tbl_emp_contract;

	public $status = array(
		'-' => '请选择',
		'1' => '有效',
		'2' => '无效',
	
		'9' => '其它',
	);
	
	public $orderby = array(
		'-' => '请选择',
		'name' => '名称',
	
		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'emp/contract/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {		
		$date1_s = $request['date1_s'];
		$date2_s = $request['date2_s'];

		$my = $this->_my_add();
		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}
		
		if (!empty($date1_s)) {
			$where .= " AND t1 >= '" . $date1_s . "'";
		}

		if (!empty($date2_s)) {
			$where .= " AND t2 < '" . $date2_s . "'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		// $this->_modify();
		// $this->_delete();
		$this->_list();

		$controller->display($request, 'emp/ldap/list');
		return true;
	}
	
	function _connect() {
		$host = "172.16.1.128";
		$host = "ldap.tecmold.net";
		// $host = "10.8.3.66";
		$port = 389;

		$ds = ldap_connect($host, $port) or die("Could not connect to {$host}");
		if ($ds) {
		    ldap_bind($ds, "cn=root,dc=tecmold,dc=com", "jeaf");
		}
		
		return $ds;
	}
	
	function _list() {
		$ds = $this->_connect();
			
	    $basedn = "ou=people,dc=tecmold,dc=com";
		$justthese = array("ou");
		
		$sr = ldap_list($ds, $basedn, "ou=*", $justthese);
		
		$info = ldap_get_entries($ds, $sr);
		
		for ($i = 0; $i < $info["count"]; $i++) {
			echo $info[$i]["ou"][0];
		}
	    
	    ldap_close($ds);
	}
	
	function _add($id, $password, $request) {
		$book = trim($request['book'], "-");
		if ($book <> '1') {
			$this->_delete($request);
			return;
		}
		
		$ds = $this->_connect();
		
		if ($ds) {		
		    $info["uid"] = $id;
		    $info["userPassword"] = $password;
		
			$info["cn"] = $request['name'];
		    $info["sn"] = $request['name'];
		    $info["displayName"] = $request['name'];
		    
			if (!empty($request['job'])) {
		    	$info["title"] = $request['job'];
		    } else {
		    	$info["title"] = array();
		    }

		    if (!empty($request['exten'])) {
		    	$info["telephonenumber"] = $request['exten'];
		    }
		    
		    if (!empty($request['short'])) {
		    	$info["homePhone"] = $request['short'];
		    }
		    
		    if (!empty($request['email'])) {
		    	$info["mail"] = $request['email'];
		    }
		    
		    $dept = $this->_input($request['dept']);
		    $info["ou"] = $this->_dept($dept);		    
		    // $info["o"] = '景荣精密产品（深圳）有限公司';
		    $info["o"] = $this->_company($request['office']);
		    
		    $info["objectclass"] = "inetOrgPerson";
		    
		    $ou = substr($request['dept'], 0, 2);		
		    $user = sprintf("uid=%s,ou=%s,ou=employee,dc=tecmold,dc=com", $id, $ou);
		    $r = ldap_add($ds, $user, $info);
		
		    ldap_close($ds);
		} else {
		    echo "Unable to connect to LDAP server"; 
		}
	}
	
	function _abcd($ds, $row) {
		$row['dept_t'] = $this->_dept($row['dept']);
			
		$info = array();
		$info["uid"] = $row['id'];
		$info["userPassword"] = $row['password'];
			
		$info["cn"] = $row['name'];
		$info["sn"] = $row['name'];
		$info["displayName"] = $row['name'];
		
		$info = $this->_ldap_x($info, $row);
			
		$info["objectclass"] = "inetOrgPerson";
		
		$dept = substr($row['dept'], 0, 2);
		// $user = sprintf("uid=%s,ou=%s,ou=employee,dc=tecmold,dc=com", $row['id'], $dept);
		$user = sprintf("uid=%s,ou=employee,dc=tecmold,dc=com", $row['id']);
		echo $user . "\n";
		if ($row['status'] == '1') {
			$r = ldap_list($ds, $user, "ou=*");
			print_r($info);
			if (!$r) {
				$r = ldap_add($ds, $user, $info);
				echo ".";
			} else {
				$r = ldap_modify($ds, $user, $info);
				echo "U";
			}
		} else {
			$r = ldap_delete ($ds, $user);
			echo "D";
		}
		
		echo ldap_error($ds);
	}
	
	function _ldap_x($info, $row) {
		$office = $row['office'];
	
		if ($office == '1') {
			$info["street"] = '广东省深圳市南山区南山大道1088号南园枫叶大厦8C 518054';
				
			$tel = '0755-89809672';
			$info["facsimiletelephonenumber"] = '0755-26416097';
		}
	
		if ($office == '2') {
			$info["street"] = '广东省深圳市宝安区沙井镇步涌同富裕工业区A-5地块B3栋 518104';
			$tel = '0755-88840348';
			$info["facsimiletelephonenumber"] = '0755-26416097';
		}
	
		if ($office == '3') {
			$info["street"] = '广东省东莞市清溪镇三中村顺峰路景荣厂 523651';
			$tel = '0769-39012955';
			$info["facsimiletelephonenumber"] = '0769-39012798';
		}
	
		if (!empty($row['exten'])) {
			$info["telephonenumber"] = $tel . '-'. $row['exten'];
		} else {
			$info["telephonenumber"] = $tel;
		}
		 
		if (!empty($row['job'])) {
			$info["title"] = $row['job'];
		}
	
		if (!empty($row['mobile'])) {
			$info["mobile"] = $row['mobile'];
		}
		 
		if (!empty($row['short'])) {
			$info["homePhone"] = $row['short'];
		}
		 
		if (!empty($row['email'])) {
			$info["mail"] = $row['email'];
		}
		 
		if (!empty($row['dept_t'])) {
			$info["ou"] = $row['dept_t'];
		}
		 
		$info["o"] = $this->_company($row['office']);
		
		$info["objectclass"] = "inetOrgPerson";
	
		return $info;
	}
	
	function _modify($id, $request) {
		$book = trim($request['book'], "-");
		$email = $request['email'];
		
		if ($book <> '1' && empty($email)) {
			$this->_delete($request);
			return;
		}
		
		$ds = $this->_connect();
		
		if ($ds) {
			$info["uid"] = $id;		
		    $info["cn"] = $request['name'];
		    $info["sn"] = $request['name'];
		    $info["displayName"] = $request['name'];
		    
		    $dept = $this->_input($request['dept']);
		    $info["ou"] = $this->_dept($dept);
		    // $info["o"] = '景荣精密产品（深圳）有限公司';
		    $info["o"] = $this->_company($request['office']);
		    
		    $info = $this->_ldap_x($info, $request);
		
		    $ou = substr($request['dept'], 0, 2);		
		    // $user = sprintf("uid=%s,ou=%s,ou=employee,dc=tecmold,dc=com", $id, $ou);
		    $user = sprintf("uid=%s,ou=staff,dc=ydt,dc=name", $id);
		    debug($user);
		    
		    $r = ldap_list($ds, $user, "ou=*");
		    if (!$r) {
		    	$r = ldap_add($ds, $user, $info);
		    	debug("add = " . $r);
		    } else {
		    	debug("modify");
		    	$r = ldap_modify($ds, $user, $info);
		    }
		    
		    ldap_close($ds);
		} else {
		    debug("Unable to connect to LDAP server");
		}
	}
	
	function _delete($request) {
		$ds = $this->_connect();
		
		if ($ds) {
		    $ou = substr($request['dept'], 0, 2);		
		    $user = sprintf("uid=%s,ou=%s,ou=staff,dc=ydt,dc=name", $request['id'], $ou);
		    debug($user);
		    
		    $r = ldap_list($ds, $user, "ou=*");
		    if ($r) {		
		    	$r = ldap_delete($ds, $user);
		    	debug("delete");
		    }
		
		    ldap_close($ds);
		}
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		require_once 'Emp.class.php';
		$emp = new Emp();
		$controller->assign_by_ref('emp', $emp->_view($id));

		$this->_my_emp($controller, $id);

		$controller->display($request, 'emp/contract/add');
		return true;
	}
	
	function _my_emp($controller, $id) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE emp = '%s' ORDER BY id DESC";
		$sql = sprintf($fmt, $this->tbl, $id);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {			
			$row = $this->_view(null, $row);
			
			$ret[] = $row;
		}

		$controller->assign_by_ref('my', $ret);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		$t1 = $request['t1'];
		$t2 = $request['t2'];
		$day = $request['day'];

		$name = $request['name'];
		$content = $request['content'];

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(emp, t1, t2, day, name, content, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $id, $t1, $t2, $day, $name, $content);
		// debug($sql);
		$db->query($sql);

		$this->add($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		require_once 'Emp.class.php';
		$emp = new Emp();
		$controller->assign_by_ref('emp', $emp->_view($row['emp']));

		$controller->display($request, 'emp/contract/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$t1 = $request['t1'];
		$t2 = $request['t2'];
		$day = $request['day'];

		$name = $request['name'];
		$content = $request['content'];

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET t1 = '%s', t2 = '%s', day = '%s', name = '%s', content = '%s'";
		$fmt .= ", modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $t1, $t2, $day, $name, $content, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'emp/contract/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row['d1'] = $this->date_1($row['t1'], $row['t2']);
		$row['d2'] = $this->date_1(date("Y-m-d"), $row['t2']);
		
		// $row['d3'] = $this->_t3($row['t2']);
		$row = $this->_view_t($row);
		
		return $row;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		$insql = "in ('000000000";
		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}
			
			$fmt = "UPDATE %s SET visible = 2 WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $id);
			debug($sql);
			$query = $db->query($sql);
		}
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function num($id) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT COUNT(*) t FROM %s WHERE emp = '%s'";
		$sql = sprintf($fmt, $this->tbl, $id);
		// debug($sql);
		$result = $db->query($sql);
		$row = $db->fetch_array($result);

		return $row['t'];
	}
}
?>